"use strict";

import Vue from 'vue';
import axios from "axios";
import {getToken, removeLoginInfo} from "../utils/session_storage"
// import store from '@/store'
import {MessageBox} from 'element-ui'

// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

let config = {
  // baseURL: process.env.baseURL || process.env.apiUrl || ""
  // timeout: 60 * 1000, // Timeout
  // withCredentials: true, // Check cross-site Access-Control
  baseURL: process.env.VUE_APP_MOCK ? '' : process.env.VUE_APP_BASE_API
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
  function(config) {
    // Do something before request is sent
    const token = getToken()
    if(token) {
        // let tokenInfo = getTokenInfo();
        config.headers['Authorization'] = 'Bearer ' + token;
        // config.headers['Platform-Id'] = tokenInfo.platformId;
        // config.headers['Platform-Name'] = tokenInfo.platformName;
        // config.headers['Role-Names'] = tokenInfo.roleNames;
        // config.headers['User-Id'] = tokenInfo.userId;
    }
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function(response) {
    // Do something with response data
      let data = response.data;
      if(data.code == 200) {
          return Promise.resolve(data)
      } else if(data.code == 500) {
          MessageBox({message: data.msg, title:'错误', type:'warning'})
          return Promise.reject(data);
      } else {
          return Promise.reject(data);
      }
  },
  function(error) {
    // Do something with response error
      if (error.response.status == 401) {
          MessageBox.confirm('登录状态已过期，您可以继续留在该页面，或者重新登录', '系统提示', {
                  confirmButtonText: '重新登录',
                  cancelButtonText: '取消',
                  type: 'warning'
              }
          ).then(() => {
              removeLoginInfo()
              location.href = '/index';
          })
      } else if(error.response.status == 403) {
          MessageBox({message:'当前用户无权访问此功能！请联系管理员',title:'警告', type:'warning'})
      } else if(error.response.status == 500 && error.response.data) {
          MessageBox({message: error.response.data.msg?error.response.data.msg:error.response.data, title:'提示', type:'warning'})
      } else if(error.response.status == 503 && error.response.data) {
          MessageBox({message: error.response.data.msg?error.response.data.msg:error.response.data, title:'提示', type:'warning'})
      }
      return Promise.reject(error);
  }
);

Plugin.install = function(Vue, options) {
  if(!options) options = {}
  Vue.axios = _axios;
  window.axios = _axios;
  Object.defineProperties(Vue.prototype, {
    axios: {
      get() {
        return _axios;
      }
    },
    $axios: {
      get() {
        return _axios;
      }
    },
  });
};

Vue.use(Plugin)

export default Plugin;
export {_axios as request}
